package com.jt.travel.auth.controller;

import com.jt.travel.auth.feign.SendSms;
import com.jt.travel.common.pojo.TUser;
import com.jt.travel.auth.service.UserService;
import com.jt.travel.auth.utils.RedisUtil;
import com.jt.travel.auth.utils.RespBean;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/auth")
public class TUserController {
    @Autowired
    SendSms sendSms;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    UserService userService;

    @PostMapping("/toLogin")
    @ResponseBody
    public RespBean userLogin(@RequestBody HashMap<String, String> params, HttpSession session) throws Exception {
        String username = params.get("username");
        String password = params.get("password");
        String verify_code = params.get("verify_code");
//        log.warn("username===>" + username);
//        log.warn("password===>" + password);
//        log.warn("verify_code===>" + verify_code);

        //1、比较验证码
        String session_verify = (String) session.getAttribute("verify_code");
        log.warn("session_verify===>" + session_verify);
        if (StringUtils.hasLength(verify_code) && session_verify.equalsIgnoreCase(verify_code)) {
            try {
                TUser tuser=userService.selectUser(username,password);
                session.setAttribute("loginUser",tuser);
                return RespBean.success("ok", tuser);
            } catch (Exception e) {
                throw new Exception("用户名不存在！");
            }
        } else {
            throw new Exception("验证码错误！");
        }
    }
    @ResponseBody
    @GetMapping("/getCaptcha")
    public void get_captcha(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        // 三个参数分别为宽、高、位数
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        // 设置字体
        specCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32));  // 有默认字体，可以不用设置
        // 设置类型，纯数字、纯字母、字母数字混合
        specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);

        String text = specCaptcha.text();
        System.out.println("验证码=========>" + text);
        session.setAttribute("verify_code", text);

        // 输出图片流
        CaptchaUtil.out(specCaptcha, request, response);
    }
    @RequestMapping("/sendsms")
    @ResponseBody
    public Map sendsms(@RequestBody HashMap<String, String> params){
        String phone = params.get("phone");
        //1、接口防刷
        String redisCode = (String) redisUtil.get("sms:code:"+phone);
        if (!StringUtils.isEmpty(redisCode)) {
            //活动存入redis的时间，用当前时间减去存入redis的时间，判断用户手机号是否在60s内发送验证码
            long currentTime = Long.parseLong(redisCode.split("_")[1]);
            if (System.currentTimeMillis() - currentTime < 60000) {
                Map map=new HashMap();
                map.put("data","验证码不能重复发送");
                //60s内不能再发
                return map ;
            }
        }
        //2、验证码的再次效验 redis.存key-phone,value-code
        int code = (int) ((Math.random() * 9 + 1) * 100000);
        String redisStorage =code + "_" +System.currentTimeMillis();
        redisUtil.set("sms:code:"+phone,redisStorage,600);
        System.out.println("---------redisStorage.split(\"_\")[0]-----------");

        Map send = sendSms.send(phone,redisStorage.split("_")[0]);

        return send;
    }
    @GetMapping("/toLogout")
    @ResponseBody
    public RespBean toLogout(HttpSession session){
        session.removeAttribute("loginUser");
        return RespBean.success("ok");
    }
}
